from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import gradio as gr
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained("./OLMo-7B", trust_remote_code=True)  
olmo = AutoModelForCausalLM.from_pretrained(
    "./OLMo-7B",
    low_cpu_mem_usage=True,
    trust_remote_code=True
).to(device).eval()

def olmo_inference(text):
    inputs = tokenizer([text], return_tensors='pt', return_token_type_ids=False)
    with torch.no_grad():
        response = olmo.generate(**inputs, max_new_tokens=250, do_sample=True, top_k=50, top_p=0.95)
        return tokenizer.batch_decode(response, skip_special_tokens=True)[0]
    return "error"

# 创建 Gradio 接口
iface = gr.Interface(
    fn=olmo_inference,
    inputs="text",
    outputs="text",
    title="pipline调用olmo模型",
    description="输入文本，模型调用進行文本補全。"
)

# 启动服务
iface.launch(server_name="0.0.0.0", server_port=7860)

